package com.lbloca.locate.utils;

import android.app.Activity;
import android.content.Intent;


import com.lbloca.locate.R;
import com.lbloca.locate.beans.PayParamBean;
import com.lbloca.locate.biz.Config;
import com.lbloca.locate.ui.activity.PayActivity;
import com.lbsw.stat.LBStat;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;


/**
 * 支付相关类
 */

public class PayUtils {

    private Activity mActivity;
    private static String mPrice = Config.PAY_VIP_LIFELONG_PRICE;
    private static String orderNo;
    private static String mPayContent, mPayType;

    public PayUtils() {

    }

    public PayUtils(Activity activity) {
        this.mActivity = activity;
    }

    public void payMoney(String name, String price, String payContent, String payType, int vipType) {
        this.mPrice = price;
        this.mPayContent = payContent;
        this.mPayType = payType;
        if (mActivity != null) {
            orderNo = getOrderNo();
            pay(payType, vipType, name);
        }
    }

    private String getOrderNo() {
        return "SF_" + System.currentTimeMillis() + RandomUtils.getRandom(1000, 9999);
    }

    private void saveStatus() {
        ToastUtils.showShort(mActivity.getString(R.string.pay_success));
        if (mPayContent.contains(Config.PAY_VIP)) {
            SPUtils.getInstance().put(Config.SPF_VIP_STATE, 1);
        }
        if (mCallback != null)
            mCallback.onPaySuccess();
        mCallback = null;
        mPayContent = mPayType = orderNo = null;
    }

    public void onPaySuccess() {
        LBStat.pay(mPayType, orderNo, true, mPayContent, Float.parseFloat(mPrice), mPayType + " sdk");
        if (mPayContent.contains(Config.PAY_VIP)) {
            SPUtils.getInstance().put(Config.SPF_VIP_STATE, 1);
        }
        if (mCallback != null)
            mCallback.onPaySuccess();
        mCallback = null;
        mPayContent = mPayType = orderNo = null;
    }

    public void onPayFailure() {
        if (mCallback != null)
            mCallback.onPayFailure();
        mCallback = null;
        mPayContent = mPayType = orderNo = null;
    }

    private void pay(final String payType, int vipType, final String name) {
        // 微信支付
        if (payType.equals(Config.PAYTYPE_WEIXIN)) {
            if (!AppUtils.isWeixinAvilible()) {
                ToastUtils.showShort(mActivity.getString(R.string.no_wechat));
                return;
            } else {
                startPay(payType, vipType, name);
            }
            // 支付宝支付
        } else if (payType.equals(Config.PAYTYPE_ALI)) {
            if (!AppUtils.isAlipayAvilible()) {
                ToastUtils.showShort(mActivity.getString(R.string.no_alipay));
                return;
            } else {
                startPay(payType, vipType, name);
            }
        }
    }

    private void startPay(String payType, int vipType, String name) {
        String payTag = "gk-app";
        if (payType.equals(Config.PAYTYPE_WEIXIN)) {
            try {
                int random = RandomUtils.getRandom(100);
                JSONObject object = new JSONObject(SPUtils.getInstance().getString(Config.SPF_WXPAY_TYPES, ""));
                for (Iterator<String> keys = object.keys(); keys.hasNext(); ) {
                    payTag = keys.next();
                    int value = object.optInt(payTag, 0);
                    if (value >= random) {
                        break;
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        Intent payIntent = new Intent(mActivity, PayActivity.class);
        PayParamBean paramBean = new PayParamBean(orderNo, name, mPrice, payType, vipType, mPayContent, payTag);
        payIntent.putExtra(PayActivity.ARG_PARAMS, paramBean);
        mActivity.startActivity(payIntent);
        // 回调
        PayActivity.setOnPayListener(new IPayUtilCallback() {
            @Override
            public void onPaySuccess() {
                saveStatus();
            }

            @Override
            public void onPayFailure() {
                if (mCallback != null)
                    mCallback.onPayFailure();
                mCallback = null;
                mPayContent = mPayType = orderNo = null;
            }
        });
    }


    private static IPayUtilCallback mCallback;

    public interface IPayUtilCallback {
        void onPaySuccess();

        void onPayFailure();
    }

    public void setIPayUtilCallback(IPayUtilCallback callback) {
        this.mCallback = callback;
    }
}